﻿using System;
using System.Web;
using System.Web.UI.WebControls;
using Subgurim.Controles;

public partial class UI_edit_photos : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (AppUser.currUser() == 0)
                Response.Redirect("../login.aspx?Retuenurl=" + Request.Url.ToString());
            if (!IsPostBack)
            {
                fillControl();
            }
            if (fu.IsPosting)
                this.managePost();
        }
        catch (Exception ex) { }
    }

    private void managePost()
    {
        try
        {
            HttpPostedFileAJAX pf = fu.PostedFile;

            if (pf.ContentType.Equals("image/gif") ||
                pf.ContentType.Equals("image/jpg") ||
                pf.ContentType.Equals("image/jpeg") ||
                pf.ContentType.Equals("image/pjpeg") ||
                pf.ContentType.Equals("image/png") ||
                pf.ContentType.Equals("image/bmp"))
            {
                if (pf.ContentLength <= 10 * 1024 * 1024 && AppUser.currUser() > 0)
                {
                    string Filename = Util.RandomString() + DateTime.Now.Millisecond.ToString() + System.IO.Path.GetExtension(pf.FileName);
                    fu.SaveAs("~/Upload/users/", Filename);
                    Util.SaveTNShotImage(180, 180, Server.MapPath("~/Upload/users/" + Filename), "~/Upload/users", Filename);
                    
                    System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(Server.MapPath(string.Concat("~/Upload/users/", Filename)));
                    System.Drawing.ImageConverter converter = new System.Drawing.ImageConverter();
                    Bintje.Image img = new Bintje.Image();
                    img.FileName = Filename;
                    img.ImageData = (byte[])converter.ConvertTo(bmp, typeof(byte[]));
                    img.Height = bmp.Height;
                    img.Width = bmp.Width;

                    Bintje.User user = new Bintje.User((Bintje.bintje.imageoSession)Session["Session"]);
                    user.LoadDetails();
                    img.UserId = user.Id;
                    user.UserImage = img;
                    user.Update();
                }
            }
        }
        catch (Exception ex) { }
    }

    public void fillControl()
    {
        try
        {
            Bintje.bintje.imageoSession session = (Bintje.bintje.imageoSession)Session["Session"];
            Bintje.User user = new Bintje.User(session);
            
            if (user.LoadDetails() && !string.IsNullOrEmpty(user.ImageUrl))
            {
                Imguser.ImageUrl = user.ImageUrl;
                btnDelete.CommandArgument = user.Id.ToString();
            }
            else
            {
                Imguser.ImageUrl = string.Empty;
                btnDelete.CommandArgument = "0";
            }
        }
        catch (Exception ex) { }
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        try
        {
            LinkButton btnDel = (LinkButton)sender;
            int imgId = 0;
            int.TryParse(btnDel.CommandArgument, out imgId);
            if (imgId > 0)
            {
                Bintje.bintje.imageoSession session = (Bintje.bintje.imageoSession)Session["Session"];
                Bintje.User user = new Bintje.User(session);
                if (null != user) 
                {
                    user.UserImage = null;
                    user.Update();
                }
                fillControl();
            }
        }
        catch (Exception ex) { }
    }
    
    protected void btnRefresh_click(object sender, EventArgs e)
    {
        Response.Redirect(Request.Url.ToString());
    }
}
